package com.augurit.gzcgwsp.qyzz.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import jxl.Sheet;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.VerticalAlignment;
import jxl.read.biff.BiffException;
import jxl.write.WritableCellFormat;
import jxl.write.WritableSheet;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dozer.DozerBeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.augurit.gzcgwsp.excelimportexport.util.ExcelUtil;
import com.augurit.gzcgwsp.qyzz.dao.YwRqQyzzOtherEvidenceDao;
import com.augurit.gzcgwsp.qyzz.entity.YwRqQyzzAzWxRyZs;
import com.augurit.gzcgwsp.qyzz.entity.YwRqQyzzFinalAzWxRyZs;
import com.augurit.gzcgwsp.qyzz.entity.YwRqRyzzFinalOtherEvidence;
import com.augurit.gzcgwsp.qyzz.entity.YwRqRyzzOtherEvidence;
import com.augurit.gzcgwsp.qyzz.service.IYwRqQyzzFinalOtherEvidenceService;
import com.augurit.gzcgwsp.qyzz.service.IYwRqQyzzOtherEvidenceService;

/**
 * 
 *  企业资质--其它证明材料ervice实现类
 * @author 张军
 *
 */
@Service
@Transactional
public class YwRqQyzzOtherEvidenceServiceImpl  implements IYwRqQyzzOtherEvidenceService {
	private final static Log log=LogFactory.getLog(YwRqQyzzOtherEvidenceServiceImpl.class);
	 @Autowired
	private YwRqQyzzOtherEvidenceDao ywRqQyzzOtherEvidenceDao;
    @Autowired
    private IYwRqQyzzFinalOtherEvidenceService ywRqQyzzFinalOtherEvidenceService;

	public void saveEntity(YwRqRyzzOtherEvidence entity) {
		ywRqQyzzOtherEvidenceDao.save(entity);
		
	}


	/*
	 * 导入其它证明材料
	 * @see com.augurit.gzcgwsp.qyzz.service.IYwRqQyzzJsRyService#importQyzzOtherEvidence(jxl.Sheet, java.lang.String, long, java.lang.String)
	 */
public String importQyzzOtherEvidence(Sheet sheet, String acceptNo, long qyid,
		String username,Map map) {
	String errors="";
	String process=(String) map.get("process");
	if("cksa".equals(process)){
	    //新增
	    int rows=sheet.getRows();
	    for(int i=2;i<rows;i++){
	        String ename=ExcelUtil.readData(sheet, 1, i);//材料名称
	        String remark=ExcelUtil.readData(sheet, 2, i);//型号
	        if(isHasData(ename,remark)){
	            YwRqRyzzFinalOtherEvidence entity=new YwRqRyzzFinalOtherEvidence();
	            entity.setQyid(qyid);
	            entity.setEname(ename);
	            entity.setRemark(remark);;
	            try {
	                ywRqQyzzFinalOtherEvidenceService.saveEntity(entity);
	            } catch (Exception e) {
	                log.error("第五个工作表"+"第"+i+"行出现错误"+e);
	                errors+="第五个工作表"+"第"+i+"行出现错误\n";               
	            }
	        }else{
	            //errors+="第五个工作表"+"第"+i+"行有空值\n";  
	        }
	    }
	  
	}else{
	    List<YwRqRyzzOtherEvidence> list=ywRqQyzzOtherEvidenceDao.getOtherEvidences(qyid,acceptNo);
	    if(list!=null && list.size()>0){
	        for(YwRqRyzzOtherEvidence entity:list){
	            //先删除
	            ywRqQyzzOtherEvidenceDao.delete(entity);
	        }
	    }
	    //新增
	    int rows=sheet.getRows();
	    for(int i=2;i<rows;i++){
	        String ename=ExcelUtil.readData(sheet, 1, i);//材料名称
	        String remark=ExcelUtil.readData(sheet, 2, i);//型号
	        if(isHasData(ename,remark)){
	            YwRqRyzzOtherEvidence entity=new YwRqRyzzOtherEvidence();
	            entity.setAcceptNo(acceptNo);//受理编号
	            entity.setQyid(qyid);
	            entity.setEname(ename);
	            entity.setRemark(remark);;
	            try {
	                ywRqQyzzOtherEvidenceDao.save(entity);
	            } catch (Exception e) {
	                log.error("第五个工作表"+"第"+i+"行出现错误"+e);
	                errors+="第五个工作表"+"第"+i+"行出现错误\n";               
	            }
	        }
	}

	}
	return errors;
}
	
	/**
	 * 判断是否有数据
	 * @param name
	 * @param remark
	 * @return
	 */
	public boolean isHasData(String name,String remark){
		if(StringUtils.isNotBlank(name)&&StringUtils.isNotBlank(remark))
			return true;
		else 
			return false;
		
	}
	
	
	  public static void main(String[] args) throws BiffException, IOException{
		  String filepath="c:\\安装维修企业资质申请表922.xls";
		   File file=new File(filepath);
			Workbook wb = Workbook.getWorkbook(file);
			Sheet sheet=wb.getSheet(2);
			int rowss=sheet.getRows();
			System.out.print(rowss);
			String dwmc=ExcelUtil.readData(sheet,1, 2);//单位名称
			System.out.print(dwmc);
	   }


	/*
	 * excel填充数据
	 * @see com.augurit.gzcgwsp.qyzz.service.IYwRqQyzzOtherEvidenceService#copyWritableSheet(jxl.write.WritableSheet, java.lang.String, long)
	 */
	public void copyWritableSheet(WritableSheet wsSix, String acceptNo,
			long qyid,String type) {
	    if("qyxx".equals(type)){
	        List<YwRqRyzzFinalOtherEvidence> list=ywRqQyzzFinalOtherEvidenceService.getOtherEvidenceService(qyid,acceptNo);
	        for(int i=0;i<list.size();i++){
	            YwRqRyzzFinalOtherEvidence entity=list.get(i);
	            String ename=entity.getEname();//其他证明材料名称
	            String remark=entity.getRemark();//备注
	            try {
	                WritableCellFormat wcf = new WritableCellFormat();
	                wcf.setBorder(Border.ALL, BorderLineStyle.THIN);
	                wcf.setAlignment(Alignment.CENTRE);
	                wcf.setVerticalAlignment(VerticalAlignment.CENTRE); 
	                ExcelUtil.modiStrCell2(wsSix, 0,  i+2, i+1+"", wcf);
	                ExcelUtil.modiStrCell2(wsSix, 1,  i+2, ename, wcf);
	                ExcelUtil.modiStrCell2(wsSix, 2,  i+2, remark, wcf);
	            } catch (Exception e) {
	                log.error("受理编号"+acceptNo+"导出主要设备与检测设备错误"+e);
	            } 
	     }
	    }else{
	        List<YwRqRyzzOtherEvidence> list=ywRqQyzzOtherEvidenceDao.getOtherEvidences(qyid,acceptNo);
	        for(int i=0;i<list.size();i++){
	            YwRqRyzzOtherEvidence entity=list.get(i);
	            String ename=entity.getEname();//其他证明材料名称
	            String remark=entity.getRemark();//备注
	            try {
	                WritableCellFormat wcf = new WritableCellFormat();
	                wcf.setBorder(Border.ALL, BorderLineStyle.THIN);
	                wcf.setAlignment(Alignment.CENTRE);
	                wcf.setVerticalAlignment(VerticalAlignment.CENTRE); 
	                ExcelUtil.modiStrCell2(wsSix, 0,  i+2, i+1+"", wcf);
	                ExcelUtil.modiStrCell2(wsSix, 1,  i+2, ename, wcf);
	                ExcelUtil.modiStrCell2(wsSix, 2,  i+2, remark, wcf);
	            } catch (Exception e) {
	                log.error("受理编号"+acceptNo+"导出主要设备与检测设备错误"+e);
	            } 
	     }
	    }

		
	}


    public Map importFinalData(Long qyid, String acceptNo) {
        List<YwRqRyzzOtherEvidence> list=ywRqQyzzOtherEvidenceDao.getOtherEvidences(qyid,acceptNo);
        for(YwRqRyzzOtherEvidence entity:list){
            YwRqRyzzFinalOtherEvidence finalEntity = new DozerBeanMapper().map(entity, YwRqRyzzFinalOtherEvidence.class);          
            finalEntity.setId(null);
            this.ywRqQyzzFinalOtherEvidenceService.saveEntity(finalEntity);
        }
        return null;
    }



	
	 
}
